layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 排班主表管理
     */
    var ScheduleInfo = {
        tableId: "scheduleDetailTable"
    };

    /**
     * 初始化表格的列
     */
    ScheduleInfo.initColumn = function () {
        return [[
            {field: 'empName', title: '员工姓名'},
            {field: 'actualHour', title: '工时'},
            {field: 'position', title: '职务'},

            {field: 'clock0830', title: '8点半'},
            {field: 'clock0900', title: '9点'},
            {field: 'clock0930', title: '9半'},
            {field: 'clock1000', title: '10点'},
            {field: 'clock1030', title: '10点半'},
            {field: 'clock1100', title: '11点'},
            {field: 'clock1130', title: '11点半'},
            {field: 'clock1200', title: '12点'},
            {field: 'clock1230', title: '12点半'},
            {field: 'clock1300', title: '13点'},
            {field: 'clock1330', title: '13点半'},
            {field: 'clock1400', title: '14点'},
            {field: 'clock1430', title: '14点半'},
            {field: 'clock1500', title: '15点'},
            {field: 'clock1530', title: '15点半'},
            {field: 'clock1600', title: '16点'},
            {field: 'clock1630', title: '16点半'},
            {field: 'clock1700', title: '17点'},
            {field: 'clock1730', title: '17点半'},
            {field: 'clock1800', title: '18点'},
            {field: 'clock1830', title: '18点半'},
            {field: 'clock1900', title: '19点'},
            {field: 'clock1930', title: '19点半'},
            {field: 'clock2000', title: '20点'},
            {field: 'clock2030', title: '20点半'},
            {field: 'clock2100', title: '21点'},
            {field: 'clock2130', title: '21点半'},
            {field: 'clock2200', title: '22点'},
            {field: 'clock2230', title: '22点半'},
            {field: 'clock2300', title: '23点'},
            {field: 'clock2330', title: '23点半'},
            {field: 'clock2400', title: '24'},

        ]];
    };
    ScheduleInfo.onBack = function (){
        window.location.href = Feng.ctxPath + '/scheduleInfo/list';
    },
    ScheduleInfo.openAttendanceDlg = function (){
        var id = document.getElementById("id").innerHTML
        layer.open({
            type: 2,
            title: '考勤明细',
            area: ['1000px', '550px'],
            anim: 5,
            content: Feng.ctxPath + '/scheduleAttendance/detailsIndex?id='+id,
            end: function () {
            }
        });
    },
    /**
     * 点击查询按钮
     */
    ScheduleInfo.search = function () {
        var queryData = {};
        if($("#plant_date").val() != ""){
            queryData['plantDate'] = $("#plant_date").val();
        }

        queryData['storeName'] = $("#store_name").val();
        debugger;
        table.reload(ScheduleInfo.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ScheduleInfo.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/scheduleInfo/add';
    };

    /**
     * 导出excel按钮
     */
    ScheduleInfo.exportExcel = function () {
        var checkRows = table.checkStatus(ScheduleInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/scheduleInfo/edit?id=' + data.id;
    };

    /**
     * 点击
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.onDetailItem = function (data) {
        window.location.href = Feng.ctxPath + '/scheduleInfo/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleInfo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ScheduleInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({

        elem: '#' + ScheduleInfo.tableId,
        url: Feng.ctxPath + '/scheduleInfo/plantDetail?id='+window.location.href.split('=')[1],
        page: false,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 60,
        cols: ScheduleInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ScheduleInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ScheduleInfo.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ScheduleInfo.exportExcel();
    });

    // 考勤详情
    $('#attendanceActualHour').click(function () {
        ScheduleInfo.openAttendanceDlg();

    });

    // 工具条点击事件
    table.on('tool(' + ScheduleInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'back') {
            ScheduleInfo.onBack();
        } else if (layEvent === 'delete') {
            ScheduleInfo.onDeleteItem(data);
        } else if (layEvent === 'detail') {
            ScheduleInfo.onDetailItem(data);
        }
    });
});
